package com.taotao.portal.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.portal.pojo.SearchResult;

@Service
public class SearchServiceImpl implements SearchService {
	// 注入搜索服务url
	@Value("${SEARCH_BASE_URL}")
	private String SEARCH_BASE_URL;

	/**
	 * 调用搜索服务
	 */
	@Override
	public SearchResult serach(String keywords, int page, int rows) {
		// 设置httpClient调用服务的参数
		Map<String, String> param = new HashMap<>();
		param.put("keywords", keywords);
		param.put("page", page + "");
		param.put("rows", rows + "");
		// 通过HttpClient发送get请求服务  返回json字符串
		String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
		// 转化为TaotaoResult对象
		TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json,
				SearchResult.class);
		// 获取searchResult对象
		SearchResult result = (SearchResult) taotaoResult.getData();
		return result;
	}

}
